package com.sskj.gcc.util;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.luck.picture.lib.tools.ScreenUtils;
import com.sskj.gcc.R;

public class PhoneNumSelectDialog extends DialogFragment {

    private RecyclerView recyclerView;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.AppThemeNoAction);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View view = inflater.inflate(R.layout.app_dialog_phone_num,null);
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        WindowManager.LayoutParams lp = getDialog().getWindow().getAttributes();
        lp.gravity = Gravity.BOTTOM;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = ScreenUtils.getScreenHeight(getActivity())*2/5 ;
        getDialog().getWindow().setAttributes(lp);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = view.findViewById(R.id.rv_dialog_phone_num);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(new PhoneNumAdapter(getActivity()));
        ((PhoneNumAdapter)recyclerView.getAdapter()).setPhoneNumSelectListener(new PhoneNumSelectListener() {
            @Override
            public void onSelectNum(String name,String num) {
                if(phoneNumSelectListener!=null){
                    phoneNumSelectListener.onSelectNum(name,num);
                }
                dismiss();
            }
        });
    }

    private PhoneNumSelectListener phoneNumSelectListener;

    public void setPhoneNumSelectListener(PhoneNumSelectListener phoneNumSelectListener) {
        this.phoneNumSelectListener = phoneNumSelectListener;
    }
}
